package com.xinya.taobaosale.ui.presenter.impl;

import android.util.Log;

import com.xinya.taobaosale.model.ApiException;
import com.xinya.taobaosale.model.api.ApiHelper;
import com.xinya.taobaosale.model.domain.HomePagerDiscoveryBean;
import com.xinya.taobaosale.model.domain.ResultBean;
import com.xinya.taobaosale.ui.presenter.interfaces.IHomeCallback;
import com.xinya.taobaosale.ui.presenter.interfaces.IHomePagerCallback;
import com.xinya.taobaosale.ui.presenter.interfaces.IHomePagerPresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @ClassName: HomePagerPresenterImpl$
 * @Description: java类作用描述
 * @Author: xinya
 * @CreateDate: 2021/2/3$ 11:02$
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public class HomePagerPresenterImpl implements IHomePagerPresenter<IHomePagerCallback> {

    private static final String TAG = "HomePagerPresenterImpl";
    public static HomePagerPresenterImpl INST = new HomePagerPresenterImpl();

    // 用来管理homePager的当前页
    private Map<Integer, Integer> pageMap = new HashMap<>();
    private static final int DEFAULT_PAGE = 1;

    private HomePagerPresenterImpl() {
    }

    @Override
    public void getHomePagerDiscoveryOrMore(int id) {
        Integer pageCurrent = pageMap.get(id);
        if (pageCurrent == null) {
            Log.d(TAG, "getHomePagerDiscoveryOrMore: " + id + " --> " + DEFAULT_PAGE);
            getHomePagerDiscovery(id, DEFAULT_PAGE);
            return;
        }
        pageCurrent++;

        Log.d(TAG, "getHomePagerDiscoveryOrMore: " + id + " --> " + pageCurrent);
        getHomePagerDiscovery(id, pageCurrent);

    }

    @Override
    public void getHomePagerDiscovery(int id, int page) {
        pageMap.put(id, page);
        if (page == DEFAULT_PAGE) {
            for (IHomePagerCallback callback : callbackList) {
                callback.onLoading(id);
            }
        }

        ApiHelper.INST.mService.getHomePagerDiscovery(id, page)
                .enqueue(new Callback<ResultBean<List<HomePagerDiscoveryBean>>>() {
                    @Override
                    public void onResponse(Call<ResultBean<List<HomePagerDiscoveryBean>>> call,
                                           Response<ResultBean<List<HomePagerDiscoveryBean>>> response) {
                        ResultBean<List<HomePagerDiscoveryBean>> listResultBean = response.body();
                        if (listResultBean == null) {
                            for (IHomePagerCallback callback : callbackList) {
                                callback.onLoadEmpty(id);
                            }
                            return;
                        }

                        try {
                            List<HomePagerDiscoveryBean> dataList = listResultBean.getData();
                            Log.d(TAG, "onResponse: " + dataList.size());
                            for (IHomePagerCallback callback : callbackList) {
                                callback.onLoadSuccess(id);
                                if (page == DEFAULT_PAGE) {
                                    callback.onHomePagerDiscovery(id, dataList);
                                    continue;
                                }
                                callback.onHomePagerDiscoveryOrMore(id, dataList);
                            }
                        } catch (ApiException e) {
                            e.printStackTrace();
                            for (IHomePagerCallback callback : callbackList) {
                                callback.onLoadEmpty(id);
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<ResultBean<List<HomePagerDiscoveryBean>>> call, Throwable t) {
                        Log.e(TAG, "onFailure: " + t.getMessage());
                        for (IHomePagerCallback callback : callbackList) {
                            callback.onLoadError(id);
                        }
                    }
                });
    }


    private static List<IHomePagerCallback> callbackList = new ArrayList<>();

    @Override
    public void register(IHomePagerCallback callback) {
        if (callbackList.contains(callback)) {
            return;
        }
        callbackList.add(callback);
    }

    @Override
    public void unRegister(IHomePagerCallback callback) {
        callbackList.remove(callback);
    }
}
